package com.tanhua.manager.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manager.domain.Log;
import com.tanhua.manager.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-log",consumerGroup = "tanhua-log-consumer")
public class LogMessageListener implements RocketMQListener<String> {
    @Autowired
    private LogService logService;
    @Override
    public void onMessage(String message) {
        log.info("===处理操作日志消息===");
        log.info("topic:{},接收的消息日志：{}","tanhua-log",message);
        //1.把接受到的json数据，还原成Map对象
        Map map = JSON.parseObject(message, Map.class);
        Long userId = Long.valueOf(map.get("userId").toString());
        String type = (String) map.get("type");
        String date = (String) map.get("date");
        //2.把接收的json数据，还原成Map对象
        Log logObj = new Log();
        logObj.setUserId(userId);
        logObj.setLogTime(date);
        logObj.setType(type);
        logObj.setCreated(new Date());
        logObj.setUpdated(new Date());
        //3.把LOg对象存储到数据库里
        logService.save(logObj);
        log.info("===处理操作日志完成===");
    }
}
